package logic

import (
	"context"

	//"fmt"
	"github.com/tal-tech/go-zero/core/logx"
	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"monitor/user/model"
)

type UserInfosLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfosLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserInfosLogic {
	return UserInfosLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfosLogic) UserInfos(req types.PageReq) (*types.UsersReply, error) {
	// todo: add your logic here and delete this line
	//分页查询
	reply := []types.UserReply{}
	count, userInfos, err := l.svcCtx.UserModel.FindAll(req.Page, req.Limit)
	//fmt.Printf("count:'%d,userInfos:%#v,err = %v\n", count, userInfos, err)
	switch err {
	case nil:
		for _, val := range userInfos {
			//fmt.Printf("index = %d,userInfo:%#v\n", index, val)
			rep := types.UserReply{
				Id:       val.Id,
				Username: val.Name,
				Mobile:   val.Mobile,
			}
			reply = append(reply, rep)
		}
		return &types.UsersReply{Count: count, Reply: reply}, nil
	case model.ErrNotFound:
		return nil, errorUserNotFound
	default:
		return nil, err
	}
}
